package com.syezon.wifikey.speed;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import java.util.Random;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SpeedTestViewModel extends ViewModel {

    private MutableLiveData<Integer> netDelayLiveData = new MutableLiveData<>();
    private MutableLiveData<Double> downloadSpeedLiveData = new MutableLiveData<>();
    private MutableLiveData<Double> uploadSpeedLiveData = new MutableLiveData<>();
    private MutableLiveData<Double> currentSpeedLiveData = new MutableLiveData<>();

    public MutableLiveData<Double> getDownloadSpeedLiveData() {
        return downloadSpeedLiveData;
    }

    public MutableLiveData<Integer> getNetDelayLiveData() {
        return netDelayLiveData;
    }

    public MutableLiveData<Double> getUploadSpeedLiveData() {
        return uploadSpeedLiveData;
    }

    public MutableLiveData<Double> getCurrentSpeedLiveData() {
        return currentSpeedLiveData;
    }

    public void startTest() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> e) throws Exception {
                int netDelay = SpeedTestUtils.getNetDelay("www.baidu.com");
                netDelayLiveData.postValue(netDelay);
                e.onNext(0);
            }
        })
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        getSpeed();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getSpeed() {
        Observable.create(new ObservableOnSubscribe<Double>() {
            @Override
            public void subscribe(ObservableEmitter<Double> e) throws Exception {
                int speed = SpeedTestUtils.getSpeed("http://static.iqiyi.com/ext/common/IQIYIsetup_appstore.exe");
                double nextDouble = speed;
                double start = 0.0;
                int gap = 100;
                if (nextDouble > 0) {
                    if (nextDouble > 2 * 1024) {
                        gap = 200;
                    }
                    while (start <= nextDouble) {
                        start += gap;
                        Thread.sleep(100);
                        currentSpeedLiveData.postValue(start);
                    }
                    downloadSpeedLiveData.postValue(nextDouble);
                } else {
                    downloadSpeedLiveData.postValue(nextDouble);
                }
                e.onNext((double) speed);
            }
        })
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Double>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Double integer) {
                        getUploadSpeed(integer);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getUploadSpeed(final Double downloadSpeed) {
        Observable.create(new ObservableOnSubscribe<Double>() {
            @Override
            public void subscribe(ObservableEmitter<Double> e) throws Exception {
                Thread.sleep(2000);
                int randomStart = (int) (downloadSpeed / 2);
                Random random = new Random();
                double nextDouble = downloadSpeed + randomStart + randomStart * random.nextDouble();
                double start = 0;
                int gap = 100;
                if (nextDouble > 0) {
                    if (nextDouble > 2 * 1024) {
                        gap = 200;
                    }
                    while (start <= nextDouble) {
                        start += gap;
                        Thread.sleep(100);
                        currentSpeedLiveData.postValue(start);
                    }
                    uploadSpeedLiveData.postValue(nextDouble);
                } else {
                    uploadSpeedLiveData.postValue(nextDouble);
                }
            }
        })
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Double>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Double integer) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
